<template>
  <div class="app-container">
    <el-form  ref="queryForm" :inline="true">
      <el-form-item>
        <el-input
          v-model="dataForm.key"
          placeholder="参数名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="getDataList()"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="getDataList()">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addOrUpdateHandle()"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-col>
    </el-row>


    <el-table
      :key="tableKey"
      v-loading="dataListLoading"
      :data="dataList"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>

      <el-table-column prop="id" header-align="center" align="center" label="主键"></el-table-column>

      <el-table-column prop="clientId" header-align="center" align="center" label="客户端名称"></el-table-column>

      <el-table-column prop="accessTokenValidity" header-align="center" align="center" label="token有效期"></el-table-column>

      <el-table-column prop="refreshTokenValidity" header-align="center" align="center" label="刷新Token有效期"></el-table-column>
     
      <el-table-column prop="area" header-align="center" align="center" label="系统部署所在区域"></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="230"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="addOrUpdateHandle(scope.row.id)"
            v-hasPermi="['system:role:edit']"
          >修改</el-button>

         <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row.id)"
            v-hasPermi="['system:role:remove']"
          >删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="totalCount>0"
      :total="totalCount"
      :page.sync="pageIndex"
      :limit.sync="pageSize"
      @pagination="getDataList()"
    />

    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./table-add-or-update";

import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination

export default {
  name: "tenant",
  components: { Pagination, AddOrUpdate },
  data() {
    return {
      dataForm: {
        key: "",
      },
      dataList: [],
      totalCount: 0,
      pageIndex: 1,
      addOrUpdateVisible: false,
      tableKey: 0,
      pageSize: 10,
      dataListLoading: false,
      dataListSelections: [],
      downloadLoading: false,
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/tenant/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
        }),
      }).then((data) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalCount = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalCount = 0;
        }
        this.dataListLoading = false;
      });
    },

    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },

    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/tenant/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then((data) => {
            if (data && data.code === 0) {
              this.$notify({
                title: "Success",
                message: "Delete Successfully",
                type: "success",
                duration: 500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch((e) => e);
    },
  }
};
</script>